package com.kystar.muxplayer.manager;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;

import com.kystar.muxplayer.BaseApp;
import com.kystar.muxplayer.model.Preset;
import com.kystar.muxplayer.PresetAdapter;
import com.kystar.muxplayer.R;

import com.kystar.muxplayer.protocol.code.get.CG91_Preset;
import com.kystar.muxplayer.protocol.code.set.CS13_Preset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by peanut on 2018/8/15
 */
public class PresetLayoutManager implements View.OnClickListener ,OnItemClickListener{
    private static PresetLayoutManager sManager;
    FrameLayout mFrameLayout;
    ImageView headerImage;
    ImageView footerImage;
    ListView mListView;
    PresetAdapter mAdapter;
    List<Preset> mList = new ArrayList<>();

    public static PresetLayoutManager getPresetSingleInstance() {
        if (sManager == null) {
            sManager = new PresetLayoutManager();
        }
        return sManager;
    }

    public void init(FrameLayout frameLayout){
        mFrameLayout = frameLayout;
        View view = LayoutInflater.from(frameLayout.getContext()).inflate(R.layout.layout_preset,null);
        headerImage = view.findViewById(R.id.main_preset_header);
        footerImage = view.findViewById(R.id.main_preset_footer);
        mListView = view.findViewById(R.id.main_preset_listview);
        mAdapter = new PresetAdapter(mListView,mList);
        Preset[] presets = BaseApp.getSettings().getPresets();
        mAdapter.addAll(presets);
        mListView.setAdapter(mAdapter);
        headerImage.setOnClickListener(this);
        footerImage.setOnClickListener(this);
        mFrameLayout.addView(view);

        mListView.setOnItemClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_preset_header:
                mListView.smoothScrollBy(-30, 10);
                break;
            case R.id.main_preset_footer:
//                mAdapter.scrollDown();
                mListView.smoothScrollBy(30, 10);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BaseApp.send(CS13_Preset.createLoad(mAdapter.getItem(position).getId())).subscribe(cmd->{
            BaseApp.updateLayers();
        });
    }
}
